import { defineConfig, loadEnv } from 'vite'
import createVitePlugins from './vite/plugins'

import { createProxy } from './vite/utils'
import * as path from 'path'
const resolve = (p: string) => {
  return path.resolve(__dirname, p)
}

export default ({ command, mode }) => {
  // 是否是生产环境
  const isBuild = command === 'build'
  // 环境变量
  const envConfig = loadEnv(mode, process.cwd())
  // 配置文件中的环境变量
  const { VITE_host, VITE_port, VITE_proxy, VITE_fileName } = envConfig

  return defineConfig({
    plugins: createVitePlugins(isBuild),
    base: './',
    resolve: {
      alias: {
        '@': resolve('src'),
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    },
    server: {
      host: VITE_host || '0.0.0.0',
      port: VITE_port ? Number(VITE_port) : 3000,
      proxy: createProxy(VITE_proxy),
    },
    build: {
      outDir: VITE_fileName || 'dist',
      sourcemap: false,
      chunkSizeWarningLimit: 2000,
    },
    esbuild: {
      drop: ['console', 'debugger'],
    },
  })
}
